﻿using Maltose.MessageQueue.Publish;

namespace Maltose.MessageQueue.Extensions
{

    /// <summary>
    /// MQ发布业务计数消息队列
    /// </summary>
    public class BusinessStatisticsMQP
    {
        /// <summary>
        /// 发送数据
        /// </summary>
        BusinessStatisticsMessage StatisticsMessage = new BusinessStatisticsMessage();

        /// <summary>
        /// 发布
        /// </summary>
        readonly MessageQueuePublish publish;

        /// <summary>
        /// 队列名称 BusinessStatistics
        /// </summary>
        public string QueueName { get; set; } = "BusinessStatistics";

        /// <summary>
        /// 发布业务计数消息队列
        /// </summary>
        /// <param name="memberId">用户id</param>
        /// <param name="statisticsType">统计业务类型选择</param>
        public BusinessStatisticsMQP(int memberId, BusinessStatisticsType statisticsType)
        {
            StatisticsMessage.Member_Id = memberId;
            StatisticsMessage.StatisticsType = statisticsType;
            publish = new MessageQueuePublish();
        }

        /// <summary>
        /// 添加数量
        /// </summary>
        /// <param name="num">数量默认=1</param>
        /// <returns></returns>
        public bool AddNumber(int num = 1)
        {
            StatisticsMessage.OperationType = 1;
            StatisticsMessage.Num = num;
            return publish.SendQueueMessage(QueueName, StatisticsMessage);
        }

        /// <summary>
        /// 减少数量
        /// </summary>
        /// <param name="num">数量默认=1</param>
        /// <returns></returns>
        public bool ReduceNumber(int num = 1)
        {
            StatisticsMessage.OperationType = 0;
            StatisticsMessage.Num = num;
            return publish.SendQueueMessage(QueueName, StatisticsMessage);
        }
    }
}
